package com.wetool.model.dto;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import com.wetool.entity.Area;
import com.wetool.entity.Merchant;
import com.wetool.model.SearchParam;
import lombok.Data;

/**
 * 描述：店铺实体对象
 * @author zhangjie
 * @since 1.0
 */
@Entity(name ="shop")
@Data
public class ShopInfoDto implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column
	private String name;
	
	/** 店铺手机号 */
	@Column
	private String mobile;
	
	@Column
	private String logoUrl;	// logo图片url路径
	
	@Column
	private String address;	// 地址
	
	@Column
	private Double longitude;	// 经度
	
	@Column
	private Double latitude;	// 纬度
	
	@Transient
	private Double distance;	// 距离
	
	@Column
	private Date createDate = new Date();
	
	@OneToOne(targetEntity = Area.class, cascade = CascadeType.ALL)
	@JoinColumn(name = "area", referencedColumnName = "id")
	private Area area;
	
	@OneToOne(targetEntity = Merchant.class, cascade = CascadeType.DETACH)
	@JoinColumn(name = "merchant_id", referencedColumnName = "id")
	private Merchant merchant;
	
	@Column
	private boolean isDeleted = false;	// 删除标识
	
	// 开通网络支付标识
	@Column
	private Integer payFlag;
	
	@Column
	private String username;	// 店铺主帐号
	
	private static Double rad(Double d) {
		return d * Math.PI / 180D ;
	}
	public static Double setDistance( Double longitude, Double latitude, SearchParam param ) {
		Double EARTH_RADIUS = 6378137D;//地球半径
		Double distance = 0D;
		if (longitude == null || latitude == null) {
			return distance;
		}
	    Double radLat1 = rad(latitude);  
        Double radLat2 = rad(param.getLatitude());  
        Double a = radLat1 - radLat2;  
        Double b = rad(longitude) - rad(param.getLongitude());  
        distance = 2 * Math.asin(  
             Math.sqrt(  
                 Math.pow(Math.sin(a/2),2)   
                 + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)  
            )  
         );  
        distance = distance * EARTH_RADIUS;  
        distance = (double) (Math.round(distance * 10000) / 10000);  
		return distance;
	}
	public static Double getDistance( Double longitude, Double latitude, SearchParam param ) {
		Double EARTH_RADIUS = 6378137D;//地球半径
		Double distance = 0D;
		if (longitude == null || latitude == null) {
			return distance;
		}
	    Double radLat1 = rad(latitude);  
        Double radLat2 = rad(param.getLatitude());  
        Double a = radLat1 - radLat2;  
        Double b = rad(longitude) - rad(param.getLongitude());  
        distance = 2 * Math.asin(  
             Math.sqrt(  
                 Math.pow(Math.sin(a/2),2)   
                 + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)  
            )  
         );  
        distance = distance * EARTH_RADIUS;  
        distance = (double) (Math.round(distance * 10000) / 10000);  
		return distance;
	}
	
}



